<?php
/**
 * @author     ：lhw
 * @date       ：Created in 2019/8/4 15:43
 */

namespace app\api\service;

use app\api\model\Kind as KindModel;
use app\api\model\Drug as DrugModel;
use app\api\model\DrugImg as DrugImgModel;
use app\api\model\DrugKinds as DrugKindsModel;
use app\api\model\DrugPharmacy as DrugPharmacyModel;
use app\api\model\Pharmacy as PharmacyModel;
use think\Db;
use think\Exception;


class DrugService extends BaseService
{
    public static function KindList(){
        $list = [];
        $model = KindModel::where(['parent_id' => 0])->select();

        for($i =0 ; $i < sizeof($model) ; $i++){
            $item = [
                'parent' => $model[$i],
                'son' => KindModel::where(['parent_id' => $model[$i]['id']])->select()
            ];
            array_push($list,$item);

        }

        return $list;
    }

    public static function add($name,$part,$guige,$yongliang,$build,$has,$price,$pay_ff,$is_show,$shuoming,$imgs,$kinds,$pharmacys,$father_pharmacy){
        Db::startTrans();
        try{
            $drug_master = DrugModel::create([
                'name' => $name,
                'part' => $part,
                'guige' => $guige,
                'yongliang' => $yongliang,
                'build' => $build,
                'has' => $has,
                'price' => $price,
                'pay_ff' => $pay_ff,
                'is_show' => $is_show,
                'shuoming' => $shuoming,
                'father_pharmacy'=>$father_pharmacy
            ]);

            $id = $drug_master->id;

            //添加图片
            if(is_array($imgs)){
                foreach ($imgs as $img){
                    DrugImgModel::create(['drug_id'=>$id,'img_url'=>$img['url'],'num' => $img['num']]);
                }
            }

            if(is_array($kinds)){
                foreach ($kinds as $kind){
                    DrugKindsModel::create(['drug_id'=>$id,'kind_id' => $kind]);
                }
            }

            if(is_array($pharmacys)){
                foreach ($pharmacys as $pharmacy){
                    DrugPharmacyModel::create(['drug_id'=>$id,'pharmacy_id' => $pharmacy]);
                }
            }
            Db::commit();

            return $id;
        }catch (Exception $e){
            Db::rollback();
            throw $e;
        }
    }

    public static function update($id,$name,$part,$guige,$yongliang,$build,$has,$price,$pay_ff,$is_show,$shuoming,$imgs,$kinds,$pharmacys,$father_pharmacy){
        Db::startTrans();
        try{
            $drug_master = DrugModel::update([
                'name' => $name,
                'part' => $part,
                'guige' => $guige,
                'yongliang' => $yongliang,
                'build' => $build,
                'has' => $has,
                'price' => $price,
                'pay_ff' => $pay_ff,
                'is_show' => $is_show,
                'shuoming' => $shuoming,
                'father_pharmacy'=>$father_pharmacy
            ],['id' => $id]);

            //添加图片
            DrugImgModel::where(['drug_id' => $id])->delete();

            if(is_array($imgs)){
                foreach ($imgs as $img){
                    DrugImgModel::create(['drug_id'=>$id,'img_url'=>$img['url'],'num' => $img['num']]);
                }
            }

            DrugKindsModel::where(['drug_id' => $id])->delete();
            if(is_array($kinds)){
                foreach ($kinds as $kind){
                    DrugKindsModel::create(['drug_id'=>$id,'kind_id' => $kind]);
                }
            }

            DrugPharmacyModel::where(['drug_id' => $id])->delete();
            if(is_array($pharmacys)){
                foreach ($pharmacys as $pharmacy){
                    DrugPharmacyModel::create(['drug_id'=>$id,'pharmacy_id' => $pharmacy]);
                }
            }
            Db::commit();

            return $id;
        }catch (Exception $e){
            Db::rollback();
            throw $e;
        }
    }

    public static function select($id){
        return DrugModel::where(['father_pharmacy'=> $id])->with(['img','kinds','father','parents'])->select();
    }
}